<template>
  <div class="user-page">
    <main>
      <div class="header">
        <p class="title">用户信息</p>
        <div class="search-container">
          <div class="search-input">
            <el-input placeholder="姓名/用户名" v-model="condition"></el-input>
            <el-button class="search-btn" type="primary" @click="handleSearch">
              <i class="el-icon-search"></i>
            </el-button>
            <el-button type="primary" @click="handleRefresh">
              <i class="el-icon-refresh"></i>
            </el-button>
          </div>
          <div class="btns">
            <el-button v-if="hasTabDownload" type="primary" @click="handleDown" :loading="tmpLoading">
              模板下载
            </el-button>
            <el-button v-if="hasTabImport" type="primary" :loading="importLoading" class="upload-file">
              <input class="file" ref="file" type="file" @change="handleImport" />导入
            </el-button>
            <el-button v-if="hasTabAdd" type="primary" @click="handleAdd">
              新增
            </el-button>
            <el-button v-if="hasTabExport" type="primary" @click="handleDownload">
              导出
            </el-button>
          </div>
        </div>
      </div>
      <div class="table-container">
        <el-table
          v-loading="tableLoading"
          :data="tableData"
          row-key="uuid">
          <el-table-column label="序号" type="index" width="100"></el-table-column>
          <el-table-column prop="userName" label="姓名">
            <template slot-scope="scope">
              <span v-if="hasTableLink" class="btn" @click="$router.push('/user-detail?id=' + scope.row.uuid)">
                {{ scope.row.userName }}
              </span>
              <span v-else>{{ scope.row.userName }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="gender" label="性别"></el-table-column>
          <el-table-column prop="userId" label="用户名"></el-table-column>
          <el-table-column prop="telephoneNumber" label="联系方式"></el-table-column>
          <el-table-column prop="emailAddress" label="邮箱"></el-table-column>
          <el-table-column prop="action" label="操作">
            <template slot-scope="scope">
              <span style="display: none;"></span>
              <el-button v-if="hasTableEdit" type="primary" size="small" @click="handleEdit(scope.row)">
                编辑
              </el-button>
              <el-button v-if="hasTableDel" type="danger" size="small" @click="handleDelete(scope.row)">
                删除
              </el-button>
            </template>
          </el-table-column>
          <div class="empty-content" slot="empty">
            <i class="iconfont iconwushuju"></i>
            <p>暂无数据</p>
          </div>
        </el-table>
        <el-pagination
          v-if="total > 10"
          :page-size="pageSize"
          :current-page="pageNo"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
      <add-user
        :visible="visible"
        :type="type"
        :option="currentData"
        :loading="loading"
        @cancel="visible = false"
        @submit="handleSave">
      </add-user>
    </main>
  </div>
</template>

<script>
import AddUser from '@/components/corp/addUser';

export default {
  components: {
    AddUser
  },
  data() {
    return {
      condition: '',
      hasSearch: false,
      pageNo: 1,
      pageSize: 10,
      total: 0,
      tableData: [],
      tableLoading: false,
      importLoading: false,
      tmpLoading: false,
      loading: false,
      visible: false,
      type: '',
      currentData: {}
    };
  },
  created() {
    this.getTableData();
  },
  methods: {
    async getTableData() {
      const params = {
        pageNum: this.pageNo,
        pageSize: this.pageSize,
      };
      if (this.hasSearch) params.condition = this.condition;
      const { success, data: {
        data
      }} = await this.$Model.getUserList(params);
      if (success && data) {
        this.tableData = data.list || [];
        this.total = data.total || 0;
      }
    },
    /**
     * pageSize 改变时会触发
     * @param pageSize 每页条数
     */
    handleSizeChange(pageSize) {
      this.pageNo = 1;
      this.pageSize = pageSize;
      this.getTableData();
    },
    /**
     * currentPage 改变时会触发
     * @param pageNo 当前页
     */
    handleCurrentChange(pageNo) {
      this.pageNo = pageNo;
      this.getTableData();
    },
    clear() {
      this.pageNo = 1;
      this.pageSize = 10;
      this.total = 0;
    },
    handleSearch() {
      this.hasSearch = true;
      this.clear();
      this.getTableData();
    },
    handleRefresh() {
      this.hasSearch = false;
      this.clear();
      this.getTableData();
    },
    // 模版下载
    async handleDown() {
      this.tmpLoading = true;
      const { success, data } = await this.$Model.getUserUploadTmp();
      this.tmpLoading = false;
      if (success && data) {
        window.open(`${this.$configs.upload}${data.msg}`);
      }
    },
    // 用户数据导入
    async handleImport(e) {
      const file = e.target.files && e.target.files[0];
      if (!file || this.importLoading) return;
      const fm = new FormData();
      fm.append('file', file);
      this.importLoading = true;
      const { success } = await this.$Model.getUserTmpUpload(fm);
      this.$refs.file.value = '';
      this.importLoading = false;
      if (success) {
        this.$message.success('导入成功');
        this.getTableData();
      }
    },
    // 导出
    handleDownload() {
      const params = {
        pageNum: this.pageNo,
        pageSize: this.pageSize,
      };
      if (this.hasSearch) params.condition = this.condition;
      const url = Object.keys(params).reduce((pre, next) => {
        pre += `${next}=${params[next]}&`;
        return pre;
      }, '?');
      window.open(`${this.$configs.baseURL}/bus/user/downLoadXlsByJxl/${url}`);
    },
    // 新增
    handleAdd() {
      this.type = 'add';
      this.currentData = {};
      this.visible = true;
    },
    // 编辑
    handleEdit(row) {
      this.type = 'edit';
      this.currentData = { ...row };
      this.visible = true;
    },
    // 删除
    handleDelete(row) {
      this.$confirm('此操作将删除该用户, 是否继续?', '提示', {
        type: 'warning'
      }).then(async () => {
        const { success } = await this.$Model.delUser({
          userIds: row.uuid
        });
        if (success) {
          this.getTableData();
          this.$message.success('删除成功');
        }
      });
    },
    // 保存
    async handleSave(data) {
      const params = {
        ...this.currentData,
        ...data,
      };
      const { success } = await this.$Model.setUserInfo(params);
      if (success) {
        this.$message.success('保存成功');
        this.getTableData();
        this.visible = false;
      }
    }
  }
};
</script>

<style scoped lang="scss">
  @import "../../assets/styles/table";
  @import "../../assets/styles/dialog";

  .user-page {
    width: 100%;
    height: 100%;
    padding: 20px 30px;
    background-color: $baseBg;
    main {
      width: 100%;
      height: 100%;
      padding: 20px;
      background-color: $mainBg;
      .header {
        width: 100%;
        height: 100px;
        display: flex;
        flex-direction: column;
        justify-content: space-between;
        margin-bottom: 20px;
        .title {
          font-size: 20px;
        }
        .search-container {
          display: flex;
          justify-content: space-between;
          align-items: center;
          .search-input {
            width: 400px;
            display: flex;
            .el-input {
              margin-right: 20px;
            }
          }
        }
        .btns {
          .upload-file {
            position: relative;
            cursor: pointer;
            .file {
              position: absolute;
              width: 100%;
              height: 100%;
              top: 0;
              left: 0;
              right: 0;
              bottom: 0;
              opacity: 0;
              cursor: pointer;
            }
          }
        }
      }
      .table-container {
        width: 100%;
        height: calc(100% - 100px);
        /deep/ .el-table {
          height: calc(100% - 50px);
          .el-table__body-wrapper {
            height: calc(100% - 40px);
            overflow-y: auto;
            .btn {
              color: $mainColor;
              cursor: pointer;
              &:hover {
                text-decoration: underline;
              }
            }
          }
        }
      }
    }
  }
</style>
